﻿$(document).ready(function () {
    $("#ajax-tree").jstree({
        "json_data": {
            "ajax": {
                "type": "POST",
                "dataType": "json",
                "async": true,
                "contentType": "application/json;",
                "opts": {
                    "method": "POST",
                    "url": "/admin/menuitem/getallnodes?menuType=" + $("#Menu_Id").val()
                },
                "url": "/admin/menuitem/getallnodes?menuType=" + $("#Menu_Id").val(),
                "data": function (node) {
                    if (node == -1) {
                        return '{ "operation" : "get_children", "id" : -1 }';
                    } else {
                        return '{ "operation" : "get_children", "id" : ' + $(node).attr("id") + ' }';
                    }
                },
                "success": function (retval) {
                    return retval.d;
                }
            }
        },
        "contextmenu": {
            'items': function ($node) {
                return {
                    "Edit": {
                        "label": "Edit",
                        "action": function (obj) {
                            window.location.href = '/admin/menuitem/edit/' + obj.attr("id");
                        }
                    },
                    "Create": {
                        "label": "Create",
                        "action": function (obj) {
                            this.create(obj);
                        }
                    },
                    "Rename": {
                        "label": "Rename",
                        "action": function (obj) {
                            this.rename(obj);
                        }
                    },
                    "Delete": {
                        "label": "Delete",
                        "action": function (obj) {
                            var self = this;
                            bootbox.dialog({
                                message: "Are you sure want to delete this item?",
                                title: "Delete",
                                buttons: {
                                    success: {
                                        label: "Ok",
                                        className: "btn-success",
                                        callback: function () {
                                            self.remove(obj);
                                        }
                                    },
                                    danger: {
                                        label: "Cancel",
                                        className: "btn-danger",
                                    }
                                }
                            });
                        }
                    }
                };
            }
        },
        "ui": {
            "select_limit": 1,
        },
        "plugins": ["themes", "json_data", "dnd", "ui", "crrm", "types", "contextmenu"]
    }).bind("rename.jstree", function (e, data) {

        var node = {
            id: data.rslt.obj.attr("id"),
            newTitle: data.rslt.new_name
        };

        $.ajax({
            type: "POST",
            contentType: 'application/json',
            url: "/admin/menuitem/rename",
            data: JSON.stringify(node)
        });

    }).bind("remove.jstree", function (e, data) {

        var node = {
            id: data.rslt.obj.attr("id")
        };

        $.ajax({
            type: "POST",
            contentType: 'application/json',
            url: "/admin/menuitem/remove",
            data: JSON.stringify(node)
        });

    }).bind("create.jstree", function (e, data) {

        var nameReg = /^[0-9A-Za-z\s]+$/;
        if (!nameReg.test(data.rslt.name)) {
            $.jstree.rollback(data.rlbk);
            return;
        }

        var node = {
            parentId: data.rslt.parent.attr("id"),
            title: data.rslt.name,
            menuType: 1
        };

        $.ajax({
            type: "POST",
            contentType: 'application/json',
            url: "/admin/menuitem/create",
            data: JSON.stringify(node),
            success: function (msg) {
                data.rslt.obj.attr("id", msg.id);
            }
        });
    }).bind("move_node.jstree", function (e, data) {

        data.rslt.o.each(function (i) {
            var node = {
                id: $(this).attr("id"),
                parentId: data.rslt.np != null ? data.rslt.np.attr("id") : null
            };

            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                url: "/admin/menuitem/movenode",
                data: JSON.stringify(node)
            });
        });
    }).bind('loaded.jstree', function (e, data) {

        var depth = 1;
        data.inst.get_container().find('li').each(function (i) {
            if (data.inst.get_path($(this)).length <= depth) {
                data.inst.open_node($(this));
            }
        });

    });

    $(document).on("click", "#ajax-tree li > a", function () {
        $("#ajax-tree div.button-edit, div.button-delete").each(function () { $(this).remove(); });
        $(this).after("<div id='" + $(this).parent().attr("id") + "' class='button-edit'></div><div id='" + $(this).parent().attr("id") + "' class='button-delete'></div>");
    });

    $(document).on("click", ".button-edit", function () {
        window.location.href = '/admin/menuitem/edit/' + $(this).attr("id");
    });

    $(document).on("click", ".button-delete", function () {
        var self = $(this);
        bootbox.dialog({
            message: "Are you sure want to delete this item?",
            title: "Delete",
            buttons: {
                success: {
                    label: "Ok",
                    className: "btn-success",
                    callback: function () {

                        var node = {
                            id: self.parent().attr("id")
                        };

                        $.ajax({
                            type: "POST",
                            contentType: 'application/json',
                            url: "/admin/menuitem/remove",
                            data: JSON.stringify(node),
                            success: function (msg) {
                                if (msg.success == true)
                                    $("#ajax-tree li#" + node.id).hide(300);
                            }
                        });
                    }
                },
                danger: {
                    label: "Cancel",
                    className: "btn-danger",
                }
            }
        });
    });

});
function refresh() {
    $("#ajax-tree").jstree("refresh");
}